<?php

namespace app\common\model;

class ActivityBalance extends CUBase
{
    protected static function init()
    {
        parent::init();
        
        // 注册 before_write 事件
        self::beforeWrite(function ($data) {
        	if($data['startime'] == '') {
        		$data['startime'] = 0;
        	} else if (!is_int($data['startime'])) {
            	$data['startime'] = strtotime($data['startime']);
            }

            if($data['endtime'] == '') {
            	$data['endtime'] = 0;
            } else if(!is_int($data['endtime'])) {
            	$data['endtime'] = strtotime($data['endtime']);
            }
            return $data;
        });
    }

    protected function getStartimeAttr($value, $data)
    {
        if($data['startime'] == 0 && $data['endtime'] == 0) {
            return '长期';
        }
        if(!is_int($data['startime'])) {
            return $data['startime'];
        }
        return date('Y-m-d H:i:s', $value);
    }
 
    protected function getEndtimeAttr($value, $data)
    {
        if($data['startime'] == 0 && $data['endtime'] == 0) {
            return '长期';
        }
        if(!is_int($data['endtime'])) {
            return $data['endtime'];
        }
        return date('Y-m-d H:i:s', $value);
    }
}
